package com.chyb.cloud.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.chyb.cloud.entities.Pay;
import com.chyb.cloud.resp.ResultData;
import com.chyb.cloud.service.PayService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

/**
 * @auther zzyy
 * @create 2023-11-20 13:09
 */
@RestController
public class PayGateWayController {
    @Resource
    PayService payService;

    @GetMapping(value = "/pay/gateway/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        Pay pay = payService.selectById(id);
        return ResultData.success(pay);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/gateway/info")
    public ResultData<String> getGatewayInfo() {
        return ResultData.success("port:" + port + "gateway info test：" + IdUtil.simpleUUID());
    }


    /*网关过滤器测试*/
    @GetMapping(value = "/pay/gateway/filter")
    public ResultData<String> getGatewayFilter(HttpServletRequest request) {
        String result = "";
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement();
            String headValue = request.getHeader(headName);
            System.out.println("请求头名：" + headName + "\n 值为：" + headValue);
            if (headName.equalsIgnoreCase("chyb-name") || headName.equalsIgnoreCase("chyb-name2")) {
                result = result + headName + "：" + headValue + "  ";
            }
        }
        System.out.println("=========================================");
        System.out.println("请求参数param1："+request.getParameter("param1"));
        System.out.println("请求参数param2："+request.getParameter("param2"));
        System.out.println("=========================================");
        return ResultData.success("getGatewayFilter 过滤器 test：" + result + "：" + DateUtil.now());
    }
}


